﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TJADRICD.CollaborativeDesignPlugin.Commission;

namespace TJADRICD.CollaborativeDesignPlugin.Form
{
    public partial class CancelCommission : System.Windows.Forms.Form
    {
        private String commissionUserId;
        private String commissionUserName;
        private CommandCancelCommission cancelCommissionFuction = null;
        List<CancelCommissionUser> commissionedList = null;

        public CancelCommission(String userId, String userName)
        {
            InitializeComponent();
            commissionUserName = userName;
            commissionUserId = userId;
            cancelCommissionFuction = new CommandCancelCommission();
            commissionedList = new List<CancelCommissionUser>();
        }
        
        private void CancelCommission_Load(object sender, EventArgs e)
        {
            this.CommissionNameLabel.Text = commissionUserName;
            commissionedList = cancelCommissionFuction.GetCommissionedList(commissionUserId);

            #region 调试数据
            CancelCommissionUser c1 = new CancelCommissionUser();
            CancelCommissionUser c2 = new CancelCommissionUser();
            c1.userId = "11";
            c1.userName = "11Name";
            c2.userId = "22";
            c2.userName = "22Name";
            c2.startDate = DateTime.Now;
            c2.finishDate = Convert.ToDateTime("2013-4-25");
            commissionedList.Add(c1);
            commissionedList.Add(c2);
            #endregion

            foreach (CancelCommissionUser userItem in commissionedList)
            {
                CommissionedNameBox.Items.Add(userItem.userName);
                userItem.ComboxNameBoxIndex = CommissionedNameBox.Items.Count - 1;
            }
            StartTimePicker.Value = DateTime.Now;
            DeadLinePicker.Value = DateTime.Now;
            
        }

        private void CancelCommissionBt_Click(object sender, EventArgs e)
        {
            CancelCommissionUser commissionedUser = new CancelCommissionUser();
            if(this.CommissionedNameBox.SelectedItem != null)
                commissionedUser = commissionedList.Find(delegate(CancelCommissionUser user) { return user.ComboxNameBoxIndex == this.CommissionedNameBox.SelectedIndex; });

            
                

            if (commissionedUser == null)
            {
                MessageBox.Show("请选择被委托人");
            }
            else
            {
                cancelCommissionFuction.DeleteCommissionRecord(commissionedUser);
                this.Close();
            }
        }

        private void CommissionedNameBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            CancelCommissionUser commissionedUser = new CancelCommissionUser();
            if (this.CommissionedNameBox.SelectedItem != null)
                commissionedUser = commissionedList.Find(delegate(CancelCommissionUser user) { return user.ComboxNameBoxIndex == this.CommissionedNameBox.SelectedIndex; });

            this.StartTimePicker.Value = commissionedUser.startDate;
            this.DeadLinePicker.Value = commissionedUser.finishDate;
        }
    }
}
